<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function myNew(fn,...args){
            if(!(fn instanceof Function)) return 
            const obj = {}
            obj.__proto__ = fn.prototype
            const result = fn.call(obj,...args)
            return result instanceof Object ? result : obj
        }
        function person(name = '张三',age = 18){
            this.name = name
            this.age = age
        }
        const student1 = myNew(person,'王五',20)
        console.log(student1,'stu1')
    </script>
    <script>
        const obj = {
            name:"张三",
            age:18
        }
        Function.prototype.myCall = function(obj,...args){
            const _this = this
            const cxt = obj ?? globalThis
            const key = Symbol('key')
            cxt[key] = this
            cxt[key](...args)
            delete cxt[key]
        }
        Function.prototype.myApply = function(obj,params = []){
            const _this = this
            const cxt = obj ?? globalThis
            const key = Symbol('key')
            cxt[key] = this
            cxt[key](...params)
            delete cxt[key]
        }
        Function.prototype.myBind = function(obj,...args){
            const _this = this
            return function(params = ''){
                _this.call(obj,...args,...params)
            }
        }
        function test(){
            console.log(this,'this')
        }
        test.myCall(obj)
        test.myApply(obj)
        const result = test.myBind(obj)
        result()
    </script>
</body>

</html>